package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author shenyu
 * @email 1870943794@qq.com
 * @date 2021-06-29 13:54:46
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
  //  @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{page}/{limit}")
    //  @RequiresPermissions("product:brand:list")
    public R listTest( @PathVariable Integer page, @PathVariable Integer limit,@RequestParam Map<String, Object> params){
        Page<BrandEntity> pageParam = new Page<>(page, limit);
        IPage<BrandEntity> pageModel = brandService.selectPageVo(pageParam, params);

        return R.ok().put("page", pageModel);
    }

    /**
     * 列表
     */
    @RequestMapping("/list1/{page}/{limit}")
    //  @RequiresPermissions("product:brand:list")
    public R listTest1( @PathVariable Integer page, @PathVariable Integer limit,BrandEntity brandEntity){
        Page<BrandEntity> pageParam = new Page<>(page, limit);
        IPage<BrandEntity> pageModel = brandService.getByPage(pageParam, brandEntity);

        return R.ok().put("page", pageModel);
    }
    /**
     * 列表
     */
    @GetMapping("/list2")
    //  @RequiresPermissions("product:brand:list")
    public R listTest1(BrandEntity brandEntity){
        Page<BrandEntity> page=new Page<>(brandEntity.getPage(),brandEntity.getPageSize());
        IPage<BrandEntity> pageModel = brandService.getBrandList(page,brandEntity.getName(),brandEntity.getBrandId());

        return R.ok().put("page", pageModel);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
  //  @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:brand:save")
    public R save(@Valid @RequestBody BrandEntity brand, BindingResult result){
        if(result.hasErrors()){
                Map<String, String> map = new HashMap<>();
            //1.获取检验的错误结果
            result.getFieldErrors().forEach((item)->{
                //FieldError 获取到错误提示
                String message = item.getDefaultMessage();
                //获取错误的属性名字
                String field = item.getField();
                map.put(field,message);
            });
            return R.error(400,"提交的数据不合法").put("data",map);
        }else {
            brandService.save(brand);


        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:brand:update")
    public R update(@RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
